package com.dongqb.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.dongqb.bean.StockInfo;
import com.dongqb.common.CacheData;
import com.dongqb.dbhelper.RowMapper;
import com.dongqb.dbhelper.SqliteHelper;

public class LoadDataService {
	SqliteHelper h;
	String tablename;
	boolean flag=false;
	
	public LoadDataService() {
		try {
			System.out.println("加载数据库文件地址："+CacheData.runPatch);
			h = new SqliteHelper(CacheData.runPatch + "\\db.db3");
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			flag=false;
		}
	}
	
	/**
	 * 获取是否成功连接
	 * @return
	 */
	public boolean testConnect() {
		return flag;
	}

	public List<StockInfo> loadData(){
		try {
			return h.executeQuery("select * from StockInfo", new RowMapper<StockInfo>() {
				@Override
				public StockInfo mapRow(ResultSet rs, int index) throws SQLException {
					StockInfo item = new StockInfo();
					item.setBh(rs.getString("bh"));
					item.setMc(rs.getString("mc"));
					item.setZd("+0.0%");
					item.setXj("--");
					return item;
				}
			});
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void insertData(String bh,String mc){
		try {
			h.execute("insert into StockInfo(bh,mc)values(?,?)",bh,mc);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteData(StockInfo data){
		try {
			h.execute("delete from StockInfo where bh=?",data.getBh());
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	
}
